<template>
  <div class="multiProcess content" ref="content">
    <div class="multiProcessHeader">
      <div :class="{'step':true,'finish':current>0}" v-text="getStepText(1)"></div>
      <div :class="{'step':true,'finish':current>1}" v-text="getStepText(2)"></div>
      <div :class="{'step':true,'finish':current>=steps.length}" v-text="getStepText(3)"></div>
    </div>
    <div class="main" :style="styles">
      <slot></slot>
    </div>
    <div class="btnbox" v-if="!isSubmit">
      <div class="btn1" @click="prev" v-show="isActive"  >{{btnText1}}</div>
      <div class="btn2" @click="faceRecognition()" v-if="faceFlag">人脸识别</div>
      <div class="btn2" @click="next" v-else>{{btnText2}}</div>
    </div>
    <div class="submitBtn" v-if="isSubmit" @click="complete">返回首页</div>
  </div>
</template>

<script>
  import {AliFaceCompar} from '@/utils/ehbUtils';

  export default {
    name: "EhbMultiProcess",
    props: {
      steps: {
        type: Array,
        required: true
      },
      current: {
        type: Number,
        required: true
      },
      btnText1: {
        type: String,
        required: false,
        default: "上一步"
      },
      btnText2: {
        type: String,
        required: false,
        default: "下一步"
      },
      isSubmit: {
        type: Boolean,
        required: false,
        default: false
      }
    },
    data() {
      return {
        styles: {
          "min-height": "3rem"
        },
        isActive: true,
        faceFlag:false,
        flag:false
      }
    },
    mounted() {
      let height = this.$refs.content.offsetHeight;
      this.styles["min-height"] = "calc(" + height + "px - 1rem - 1.4rem)";
      if (this.current == 1) {
        this.isActive = false;
      } else {
        this.isActive = true;
      }

    },
    methods: {

      async faceRecognition() {
        //人脸识别
        await AliFaceCompar(this.$store.getters.userInfo,() => {
          this.faceFlag = false;
          this.flag = true;
        });
        await this.$forceUpdate();
      },

      getStepText(num) {

        if(this.$globalEnv.enableFaceID&&this.current==3){
          if(this.flag){
            this.faceFlag = false;
          }else{
            this.faceFlag = true;
          }
        }

        if (this.current == 1) {
          this.isActive = false;
        } else {
          this.isActive = true;
        }

        let index = this.current - 1;
        if (index == 0) {
          index++;
        }

        if (index >= this.steps.length - 1) {
          index = this.steps.length - 2;
        }
        switch (num) {
          case 1:
            return this.steps[--index];
          case 2:
            return this.steps[index];
          case 3:
            return this.steps[++index];
        }

      },
      next() {
        if (this.current >= this.steps.length) {
          return;
        }
        this.$emit('update:current', ++this.current);
        this.$emit("nextClick");
        if (this.current == 1) {
          this.isActive = false;
        } else {
          this.isActive = true;
        }
      },
      prev() {
        if (this.current == 1) {
          this.isActive = false;
          return;
        }
        this.$emit('update:current', --this.current);
        if (this.current == 1) {
          this.isActive = false;
        } else {
          this.isActive = true;
        }
      },
      complete() {
        this.$emit("complete");
      }
    }
  }
</script>

<style scoped>
  .multiProcessHeader {
    width: 100%;
    height: 1rem;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .multiProcessHeader .step {
    width: 1.66rem;
    height: 0.6rem;
    text-align: center;
    line-height: 0.6rem;
    font-size: 0.24rem;
    font-weight: 400;
    color: rgba(96, 104, 118, 1);
    background: url(../../assets/img/step2.png);
    background-size: 100% 0.6rem;
    background-repeat: no-repeat;
    margin-right: 0.1rem;
  }

  .multiProcessHeader .step.finish {
    color: rgba(255, 125, 0, 1);
    background: url(../../assets/img/step1.png);
    background-size: 100% 0.6rem;
    background-repeat: no-repeat;
  }

  .btnbox {
    width: 100%;
    height: 0.9rem;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 0.5rem;
  }

  .btnbox .btn1 {
    width: 2rem;
    height: 0.9rem;
    background: #FFFFFF;
    border-radius: 0.06rem;
    font-size: 0.32rem;
    font-weight: 400;
    color: rgba(61, 117, 197, 1);
    text-align: center;
    line-height: 0.9rem;
    border: 0.02rem solid rgba(61, 117, 197, 1);
  }

  .btnbox .btn2 {
    width: 3.4rem;
    height: 0.9rem;
    background: #3D75C5;
    border-radius: 0.06rem;
    font-size: 0.32rem;
    font-weight: 400;
    color: rgba(255, 255, 255, 1);
    text-align: center;
    line-height: 0.9rem;
    margin-left: 0.4rem;
  }

  .submitBtn {
    width: 100%;
    background: #3D75C5;
    border-radius: 0.06rem;
    height: 0.9rem;
    text-align: center;
    line-height: 0.9rem;
    color: #FFFFFF;
    outline: none;
    border: 0;
  }
</style>
